package finalfrontier;

import java.io.Serializable;

import java.util.Vector;
/**Classe AnimalMutant
 * @author Nicolas Roy Boourdages
 */

public class AnimalMutant extends CarreVieElite implements Comportement,
                                                           Serializable {
    
    private Vector<Coord> coordElite;
    private Vector<Coord> coordEnemies;
    private Vector<Coord> coordVides;
    
    int k, m, index;
    private Coord tempC;
    
    private int lastFrame = 0;
    private int lastFrameManger = -Constants.limiteToursMangerMutant;
    private float[] color = new float[3];
    
    public AnimalMutant(double d, double d1, double d2, String string, long seed) {
        super(d, d1, d2, string, seed);
        
        color[0] = 1.0f;
        color[1] = 1.0f;
        color[2] = 0.0f;
        
        coordElite = new Vector<Coord>(Constants.VISION_RANGE_MUTANT * Constants.VISION_RANGE_MUTANT);
        coordEnemies = new Vector<Coord>(Constants.VISION_RANGE_MUTANT * Constants.VISION_RANGE_MUTANT);
        coordVides = new Vector<Coord>(Constants.VISION_RANGE_MUTANT * Constants.VISION_RANGE_MUTANT);
    }

    public AnimalMutant() {
        super();
    }
    
    public float[] execute(int i, int j){
        if(lastFrame != DisplayWindow.frameCounter){
            buildMiniMap(i,j);
            vieillir();
            
            deciderManger(i,j);
            //manger(i,j);
            mourir(i,j);
            
        }
        lastFrame = DisplayWindow.frameCounter;
        
        return calculateColor();
    }
    
    public void buildMiniMap(int i, int j) {
        coordElite.clear();
        coordEnemies.clear();
        coordVides.clear();
        
        for (int q = -Constants.VISION_RANGE_MUTANT; q <= Constants.VISION_RANGE_MUTANT; q++){ 
            for (int w = -Constants.VISION_RANGE_MUTANT; w <= Constants.VISION_RANGE_MUTANT; w++){
                if ((i + q) >= 0 && (j + w) >= 0 && (i + q) < Constants.REAL_SIZE_X && (j + w) < Constants.REAL_SIZE_Y){
                    k = i + q;
                    m = j + w;
                    
                    tempC = new Coord(k,m);
                    if (q == 0 && w == 0){
                        
                    }
                    else if(WorldMap.isEmpty(k, m)){
                        coordVides.add(tempC);
                    }
                    else if(WorldMap.getMapVie(k, m).getClass() == AnimalElite.class){
                        coordElite.add(tempC);
                    }
                    else{
                        coordEnemies.add(tempC);
                    }
                }
            }
        }
    }
    
    public void deciderManger(int i, int j){
        if (lastFrame - lastFrameManger > Constants.limiteToursMangerMutant){
            if (coordEnemies.size() > 0){
                index = getClosest(i, j,coordEnemies);
                WorldMap.swapAndKill(coordEnemies.get(index).getX(), coordEnemies.get(index).getY(), i, j);
                lastFrameManger = lastFrame;
                setEnergie(getEnergie() - 0.3);
            }
            else{
                setEnergie(getEnergie() - 0.01);
            }
        }
        
    }

    public void manger(int i, int j) {
    }

    public void deciderReproduire() {
    }

    public void essayerReproduire(int i, int j) {
    }

    public void seReproduire(int i, int j) {
    }
    
    public float[] calculateColor(){
        return color;
    }
}
